package com.jhf.skypac_pda.base;


import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;

import androidx.appcompat.app.AppCompatActivity;

import com.jhf.skypac_pda.EventMessage.EventMessageBase;
import com.jhf.skypac_pda.R;
import com.jhf.skypac_pda.utils.ConstantPara;
import com.jhf.skypac_pda.utils.LogUtils;
import com.jhf.skypac_pda.utils.PrefUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.Locale;

import butterknife.ButterKnife;

public class BaseActivity extends AppCompatActivity {
    public AppManager appManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        // 隐藏导航栏
        View decorView = getWindow().getDecorView();
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(uiOptions);
        appManager = AppManager.getAppManager();
        // 把当前activity加入栈中
        appManager.addActivity(this);
        appManager.getSize("销毁前堆栈activity数目");
        changeLanguage();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // activity关闭，从栈中移除
        appManager.finishActivity(this);
        appManager.getSize("销毁后堆栈activity数目");
        EventBus.getDefault().unregister(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        changeLanguage();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        changeLanguage();
    }

    private void changeLanguage() {

        String language = PrefUtils.getString(this, ConstantPara.CHANGE_LANGUAGE_PREF_KEY, "zh");
        LogUtils.e("language","language=="+language);
        // 本地语言设置
        Locale myLocale = new Locale(language);
        Resources res = getResources();
        DisplayMetrics dm = res.getDisplayMetrics();
        Configuration conf = res.getConfiguration();
        conf.locale = myLocale;
        res.updateConfiguration(conf, dm);

    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetEventBusMessageOne(EventMessageBase eventMsg) {
        //空实现
    }



}

